package cn.iocoder.yudao.module.erp.service.stock.goodswarehouselink;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.stock.goodswarehouselink.vo.GoodsWarehouseLinkPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.stock.goodswarehouselink.vo.GoodsWarehouseLinkRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.stock.goodswarehouselink.vo.GoodsWarehouseLinkSaveReqVO;
import cn.iocoder.yudao.module.erp.convert.goodswarehouselink.ErpGoodsWarehouseLinkConvert;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.warehouse.WarehouseDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.goodswarehouselink.GoodsWarehouseLinkDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.warehouse.WarehouseMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.stock.goodswarehouselink.GoodsWarehouseLinkMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.GOODS_WAREHOUSE_LINK_NOT_EXISTS;

/**
 * 物品和仓库的连接 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GoodsWarehouseLinkServiceImpl implements GoodsWarehouseLinkService {

    @Resource
    private GoodsWarehouseLinkMapper goodsWarehouseLinkMapper;
    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public Long createGoodsWarehouseLink(GoodsWarehouseLinkSaveReqVO createReqVO) {
        // 插入
        GoodsWarehouseLinkDO goodsWarehouseLink = BeanUtils.toBean(createReqVO, GoodsWarehouseLinkDO.class);
        goodsWarehouseLinkMapper.insert(goodsWarehouseLink);
        // 返回
        return goodsWarehouseLink.getId();
    }

    @Override
    public void updateGoodsWarehouseLink(GoodsWarehouseLinkSaveReqVO updateReqVO) {
        // 校验存在
        validateGoodsWarehouseLinkExists(updateReqVO.getId());
        // 更新
        GoodsWarehouseLinkDO updateObj = BeanUtils.toBean(updateReqVO, GoodsWarehouseLinkDO.class);
        goodsWarehouseLinkMapper.updateById(updateObj);
    }

    @Override
    public void deleteGoodsWarehouseLink(Long id) {
        // 校验存在
        validateGoodsWarehouseLinkExists(id);
        // 删除
        goodsWarehouseLinkMapper.deleteById(id);
    }

    private void validateGoodsWarehouseLinkExists(Long id) {
        if (goodsWarehouseLinkMapper.selectById(id) == null) {
            throw exception(GOODS_WAREHOUSE_LINK_NOT_EXISTS);
        }
    }

    @Override
    public GoodsWarehouseLinkDO getGoodsWarehouseLink(Long id) {
        return goodsWarehouseLinkMapper.selectById(id);
    }

    @Override
    public PageResult<GoodsWarehouseLinkDO> getGoodsWarehouseLinkPage(GoodsWarehouseLinkPageReqVO pageReqVO) {
        return goodsWarehouseLinkMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GoodsWarehouseLinkDO> getlinksByGoodsId(Long id) {
        List<GoodsWarehouseLinkDO> goodsWarehouseLinkDOS = goodsWarehouseLinkMapper.selectList(new LambdaQueryWrapper<GoodsWarehouseLinkDO>().
                eq(GoodsWarehouseLinkDO::getGoodsId, id));
       if (goodsWarehouseLinkDOS.isEmpty()) {
            throw exception(GOODS_WAREHOUSE_LINK_NOT_EXISTS);
        }

        return goodsWarehouseLinkDOS;
    }

    @Override
    public List<GoodsWarehouseLinkRespVO> getLinkWithWarehouseNamesByGoodsId(Long goodsId) {
        List<GoodsWarehouseLinkDO> goodsWarehouseLinkDOS = goodsWarehouseLinkMapper.selectListByGoodsId(goodsId);
        List<GoodsWarehouseLinkRespVO> goodsWarehouseLinkRespVOS = ErpGoodsWarehouseLinkConvert.INSTANCE.convertList(goodsWarehouseLinkDOS);
        goodsWarehouseLinkRespVOS.forEach(item -> {
            WarehouseDO warehouseDO = warehouseMapper.selectById(item.getWarehouseId());
            item.setWarehouseName(warehouseDO.getName());
        });
        return goodsWarehouseLinkRespVOS;
    }

    @Override
    public GoodsWarehouseLinkRespVO getLinksByGoodsIdWarehouseId(Long goodsId, Long warehouseId) {
        GoodsWarehouseLinkDO goodsWarehouseLinkDO = goodsWarehouseLinkMapper.selectOneByGoodsIdWarehouseId(goodsId, warehouseId);
        return ErpGoodsWarehouseLinkConvert.INSTANCE.convert(goodsWarehouseLinkDO);
    }

}